package com.dhcc.sds.base.controller;


import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.base.entity.SdsDataConfigEntity;
import com.dhcc.sds.base.service.ISdsDataConfigService;


/**
 * 项目数据源配置表
 * 
 * @ClassName: SdsDataConfigController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-04-15 11:14:54
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsdataconfig")
public class SdsDataConfigController extends BaseController {

	private String PREFIX = "/backend/sds/sdsdataconfig/";

	@Autowired
	private ISdsDataConfigService sdsDataConfigService;
	

    /**
     * 跳转到项目数据源配置表列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsdataconfig.html";
    }
	
    /**
     * 跳转到新增项目数据源配置表
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsdataconfig_add.html";
    }
	
	/**
     * 跳转到修改项目数据源配置表
     */
    @RequestMapping("/to_update/{sdsDataConfigId}")
    public String toUpdate(@PathVariable Long sdsDataConfigId, Model model) {
        SdsDataConfigEntity sdsDataConfig = this.sdsDataConfigService.selectById(sdsDataConfigId);
        model.addAttribute("sdsDataConfig",sdsDataConfig);
        return PREFIX + "sdsdataconfig_edit.html";
    }

    /**
     * 获取项目数据源配置表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsDataConfigEntity> page = new PageFactory<SdsDataConfigEntity>().defaultPage();
        List<SdsDataConfigEntity> result = this.sdsDataConfigService.page(page,params);
        page.setRecords((List<SdsDataConfigEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 获取项目数据源配置
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/configList")
    @ResponseBody
    public Object configList(@RequestParam Map<String, Object> params) {
        Wrapper<SdsDataConfigEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("fitem_id", params.get("fitemId"));
        if (CommonUtil.isNotEmpty(params.get("itemDicId"))) {
            wrapper.eq("fitem_dic_id", params.get("itemDicId"));
        } else {
            wrapper.isNull("fitem_dic_id");
        }
        return this.sdsDataConfigService.selectList(wrapper);
    }

    /**
     * 获取项目数据源配置
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/config/{configId}")
    @ResponseBody
    public Object config(@PathVariable String configId) {
        return this.sdsDataConfigService.selectById(configId);
    }

    /**
     * 新增项目数据源配置表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsDataConfigEntity sdsDataConfig) {
        if (CommonUtil.isOneEmpty(sdsDataConfig)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsDataConfig.setUpdateDate(new Date());
        sdsDataConfig.setUpdateUser("admin");
        sdsDataConfig.insert();
        return R.ok();
    }

    /**
     * 删除项目数据源配置表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsDataConfigId) {
	
        this.sdsDataConfigService.deleteById(sdsDataConfigId);

        return R.ok();
    }

    /**
     * 修改项目数据源配置表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsDataConfigEntity sdsDataConfig) {
        if (CommonUtil.isOneEmpty(sdsDataConfig)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsDataConfigEntity old = this.sdsDataConfigService.selectById(sdsDataConfig.getId());

        old.setFitemId(sdsDataConfig.getFitemId());
        old.setFitemDicId(sdsDataConfig.getFitemDicId());
        old.setDsId(sdsDataConfig.getDsId());
        old.setGetTiming(sdsDataConfig.getGetTiming());
        old.setLinkStandId(sdsDataConfig.getLinkStandId());
        old.setCalcExp(sdsDataConfig.getCalcExp());
        old.setUpdateUser(sdsDataConfig.getUpdateUser());
        old.setBtIsCache(sdsDataConfig.getBtIsCache());
        old.setValueItemCode(sdsDataConfig.getValueItemCode());
        old.setUpdateDate(new Date());
        old.setUpdateUser("admin");
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 新增或修改项目数据源配置表
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit(SdsDataConfigEntity sdsDataConfig) {
        if (CommonUtil.isOneEmpty(sdsDataConfig)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        Date now = new Date();
        /*Wrapper<SdsDataConfigEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("fitem_id", sdsDataConfig.getFitemId());
        if (CommonUtil.isNotEmpty(sdsDataConfig.getFitemDicId())) {
            wrapper.eq("fitem_dic_id", sdsDataConfig.getFitemDicId());
        } else {
            wrapper.isNull("fitem_dic_id");
        }
        SdsDataConfigEntity old = this.sdsDataConfigService.selectOne(wrapper);*/
        SdsDataConfigEntity old = this.sdsDataConfigService.selectById(sdsDataConfig.getId());

        String calcStr = "";
        try {
            calcStr = java.net.URLDecoder.decode(sdsDataConfig.getCalcExp(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        if (CommonUtil.isEmpty(old)) {
            sdsDataConfig.setCalcExp(calcStr);
            sdsDataConfig.setUpdateDate(now);
            sdsDataConfig.setUpdateUser("admin");
            sdsDataConfig.insert();
        } else {
            old.setDsId(sdsDataConfig.getDsId());
            old.setGetTiming(sdsDataConfig.getGetTiming());
            old.setLinkStandId(sdsDataConfig.getLinkStandId());
            old.setCalcExp(calcStr);
            old.setBtIsCache(sdsDataConfig.getBtIsCache());
            old.setValueItemCode(sdsDataConfig.getValueItemCode());
            old.setUpdateDate(now);
            old.setUpdateUser("admin");
            old.updateAllColumnById();
        }
        return R.ok();
    }
	
}
